﻿namespace DreamDriven.ObjectModel.Mvp
{
    /// <summary>
    /// Common presenter for any client framework
    /// </summary>
    /// <typeparam name="TView">The type of the view.</typeparam>
    public abstract class Presenter<TView>
        where TView : class, IView
    {
        /// <summary>
        /// Gets the view instance that this presenter is bound to.
        /// </summary>
        public TView View { get; private set;}

        /// <summary>
        /// Initializes a new instance of the <see cref="Presenter{TView}"/> class.
        /// </summary>
        protected Presenter(TView view)
        {
            View = view;
        }

        /// <summary>
        /// Gets the domain model.
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <typeparam name="TDomainModel"></typeparam>
        /// <returns></returns>
        public virtual TDomainModel GetDomainModel<TService, TDomainModel>()
            where TDomainModel : IDomainModel
            where TService : IDomainService
        {
            return ServiceLocator.Instance.GetDomainModel<TService, TDomainModel>();
        }

        /// <summary>
        /// Gets the Service.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual T GetService<T>()
            where T : IDomainService
        {
            return ServiceLocator.Instance.GetService<T>();
        }

        public virtual T GetComponent<T>()
        {
            return ServiceLocator.Instance.GetComponent<T>();
        }

        protected void ReleaseServices()
        {
            ServiceLocator.Instance.Dispose();
        }
    }
}
